/**    
 * @{#} BuweiActivity.java Create on 2014-6-9 下午8:50:36    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.evan.common.pullrefresh.LoadingLayout;
import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.smiier.skin.adapter.QuestionAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.extra.UserExtra;
import com.smiier.skin.net.QuestionGetListTask;
import com.smiier.skin.net.QuestionGetListTask.QuestionGetListRequest;
import com.smiier.skin.net.QuestionGetListTask.QuestionGetListResponse;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.ui.PersonDetailView;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class PersonHomeActivity extends BasicActivity implements
		OnRefreshListener<ListView>, OnItemClickListener, OnClickListener {
	private PullToRefreshListView list_following_question;
	private QuestionAdapter questionAdapter;
	Long start_qid;
	ArrayList<Object> array = new ArrayList<Object>();
	private int mStatus = 1;
	private boolean mShowNum = true;
	private LoadingLayout mLoading;
	private User mUser;
	private PersonDetailView mHeader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.person_home);
		init();
		setNavTitle("用户主页");
		mHeader = (PersonDetailView) LayoutInflater.from(
				getApplicationContext()).inflate(R.layout.person_detial, null);
		list_following_question = (PullToRefreshListView) findViewById(R.id.list);
		list_following_question.getListView().addHeaderView(mHeader);
		list_following_question.setOnRefreshListener(this);
		list_following_question.setPullRefreshEnabled(false);
		list_following_question.setScrollLoadEnabled(true);
		mLoading = list_following_question.getFooterLoadingLayout();
		list_following_question.doPullRefreshing(true);
		list_following_question.setOnItemClickListener(this);
		UserExtra extra = new UserExtra();
		extra.getFrom(getIntent());
		mUser = extra.getUser();
	}

	@Override
	protected void onResume() {
		super.onResume();
//		MobclickAgent.onResume(this);
		mHeader.setMessage(mUser, mBitmapUtils, GlobalSettings.SERVER_IMG_URL
				+ mUser.Pic + GlobalSettings.IMG_SRC);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		start_qid = null;
		array.clear();
		loadData(false);
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		if (!mShowNum)
			loadData(false);
	}

	public void loadData(boolean isShowDialog) {

		final QuestionGetListRequest req = new QuestionGetListRequest();
		req.pageSize = Constant.pageSize;
		req.statuses = new ArrayList<Integer>();
		req.statuses.add(3);
		req.startQid = start_qid;
		// req.token = mUser.Token;
		req.uid = mUser.Uid;

		QuestionGetListTask task = new QuestionGetListTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<QuestionGetListTask.QuestionGetListRequest, QuestionGetListTask.QuestionGetListResponse>() {

			@Override
			public void onComplete(
					INetTask<QuestionGetListRequest, QuestionGetListResponse> task,
					QuestionGetListResponse response) {
				if (response.ResultCode != 200) {
					return;
				}
				if (!CommonUtility.isNull(response)) {
					try {
						JSONArray user_array = JsonUtil
								.convertToArray(response.Res.Questions);
						if (user_array.length() > 0) {
							start_qid = user_array.getJSONObject(
									user_array.length() - 1).getLong("Qid");
						}

						list_following_question.doComplete();
						try {

							if (user_array.length() > 0) {
								CommonUtility.putAll(array, user_array, true);
								if (CommonUtility.isNull(questionAdapter)) {
									questionAdapter = new QuestionAdapter(
											activity, array);
									list_following_question
											.setAdapter(questionAdapter);
								} else {
									questionAdapter.notifyUpdate();
								}
								if (user_array.length() < Constant.pageSize) {
									list_following_question
											.setHasMoreData(false);
								} else {
									list_following_question
											.setHasMoreData(true);
								}
							} else {

								list_following_question.setHasMoreData(false);
								questionAdapter.notifyUpdate();
							}

						} catch (Exception e) {
							e.printStackTrace();
						}

						if (array.size() < 1) {
							list_following_question
									.setContextEmptyType(true, 2);
						} else {
							list_following_question.setContextEmptyType(false,
									2);
							list_following_question.setHasMoreData(false);
						}

					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					list_following_question.setContextEmptyType(true, 2);
				}

			}

			@Override
			public void onException(
					INetTask<QuestionGetListRequest, QuestionGetListResponse> task,
					Exception e) {
				// TODO Auto-generated method stub
				list_following_question.setContextEmptyType(true, 2);
			}
		});
		add(task);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		if (arg2 != 0) {
			JSONObject object = (JSONObject) view.getTag(R.id.tag_obj);
			Intent intent = new Intent(activity, QuestionDetailActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY, object.toString());
			startActivity(intent);
		}
	}

	/*@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
